package sandbox;

import java.util.LinkedList;
import java.util.List;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import sandbox.domain.Foo;
import sandbox.dto.BarDto;
import sandbox.dto.EntityMappingManager;
import sandbox.dto.FooDto;
import sandbox.dto.test.FooBarDto;
import sandbox.util.LogUtils;

/** Example resource class hosted at the URI path "/test"
 */
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class MyResource {

    @Autowired
    private EntityMappingManager mappingManager;
    
    /** Method processing HTTP GET requests, producing "text/plain" MIME media
     * type.
     * @return String that will be send back as a response of type "text/plain".
     */
    @GET
    @Path("/foo")
//    @Produces("text/plain")
    public ObjectDto getIt() {
        ObjectDto subDto = new ObjectDto(new ObjectDto.DtoAttribute("b1", "BBB"),
                                        new ObjectDto.DtoAttribute("b2", false),
                                        new ObjectDto.DtoAttribute("b3", new Long[] {1L, 2L, 3L}));
        List<ObjectDto> subs = new LinkedList<ObjectDto>();
        subs.add(subDto);
        subs.add(subDto);
        ObjectDto response = new ObjectDto(new ObjectDto.DtoAttribute("a1", "AAA"),
                                            new ObjectDto.DtoAttribute("a2", 123),
                                            new ObjectDto.DtoAttribute("a3", subDto),
                                            new ObjectDto.DtoAttribute("a4", subs));
        FooBarDto foobarDto = new FooBarDto();
        foobarDto.setFoobar("foobar");
        List<FooBarDto> foobars = new LinkedList<FooBarDto>();
        foobars.add(foobarDto);
        BarDto barDto = new BarDto();
        barDto.setId(16L);
        barDto.setName("bar");
        barDto.setLongitude(3.0);
        barDto.setLatitude(4.0);
        barDto.setEnabled(false);
        barDto.setFoobars(foobars);
        FooDto fooDto = new FooDto();
        fooDto.setId(64L);
        fooDto.setBar(barDto);
        Foo foo = null;
        FooDto fooDto2;
        try {
            foo = (Foo) mappingManager.buildEntity(fooDto);
            foo.getBar().setLatitude(777.0);
            fooDto2 = (FooDto) mappingManager.buildDto(foo);
        } catch(Exception ex) {
            LogUtils.warn(this, "Failed to build entity", ex);
        }

        return response;
    }
}
